(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.resourceBundle.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'resourceBundleService', '$filter' ];
	function deleteController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, resourceBundleService, $filter) {
		var ctrl = this, resourceBundleInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.searchCriteria.resBundleId = resourceBundleInfo.resBundleId;
			ctrl.searchCriteria.refResBundleId = resourceBundleInfo.refBundleId;
			ctrl.resourceBundle = resourceBundleInfo;
			ctrl.search();
		};
		
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			if(ctrl.searchCriteria.resBundleId){
				ctrl.searchCriteria.resBundleId = $filter('encode')(ctrl.searchCriteria.resBundleId);
			}
			authService.search(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}
		
		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.resourceBundle.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.resourceBundle).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.resourceBundle.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.resourcebundle.list',
					'state' : 'cda.resourceBundle.list',
					'params' : {}
				}]
			});
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.delete' ]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);